<template>
    <div id="mima">
        <div class="box">
            <div class="one">
                <div class="logo"></div>
                <div class="title">重置密码</div>
            </div>
            <div class="two">
                <div class="input">
                    <a-form :model="formState" name="basic" :label-col="{ span: 8 }" :wrapper-col="{ span: 7 }"
                        autocomplete="off">
                        <a-form-item label="旧密码" name="password" style="display: flex;align-items: center;">
                            <a-input v-model:value="formState.password" style="height: 40px;" />
                        </a-form-item>

                        <a-form-item label="新密码" name="newpwd" style="display: flex;align-items: center;">
                            <a-input-password :disabled="!formState.password" v-model:value="formState.newpwd"
                                style="height: 40px;" />
                        </a-form-item>

                        <a-form-item label="确认密码" name="pwd" style="display: flex;align-items: center;">
                            <a-input-password :disabled="!formState.password || !formState.newpwd"
                                v-model:value="formState.pwd" style="height: 40px;" />
                        </a-form-item>
                    </a-form>
                </div>
            </div>
            <div class="three">
                <div>
                    <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
                        <a-button :disabled="!formState.password || !formState.newpwd || !formState.pwd"
                            @click="handleSubmit(formState)" type="primary" html-type="submit">保存</a-button>
                    </a-form-item>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">

// 定义 User 类型，表示用户对象的结构 
type User = {
    _id: string,
    usernmae: string,
    code: string,
    ImgUrl: string,
    phone: string,
    password: string
    status: Boolean,
    createTime: Date,
    remark: string,
    roleid: string
}

// 创建一个函数类型，返回 User 对象或字符串  
type GetUserFunction = () => User | string;

// 函数实现，用于获取用户信息  
const getUser: GetUserFunction = () => {
    const storedUser = localStorage.getItem('user');
    // 判断用户信息是否存在，如果存在则解析为对象，否则返回"未登录"  
    return storedUser ? JSON.parse(storedUser) : '未登录';
};

// 获取用户初始化数据，并将其放入 reactive 中  
let user = reactive(getUser());

import houseApi from '@/axios';
import router from '@/router';
import { message } from 'ant-design-vue';
import { reactive } from 'vue';

interface FormState {
    password: string;
    newpwd: string;
    pwd: string;
}

const formState = reactive<FormState>({
    password: '',
    newpwd: '',
    pwd: '',
});

const handleSubmit = async (value: any) => {
    if (value.password !== user.password) {
        message.error('旧密码错误');
        return;
    }
    if (value.newpwd !== value.pwd) {
        message.error('两次密码输入不一致');
        return;
    } else {
        user.password = value.newpwd;
        const { data: { code, msg } } = await houseApi.post('/changePassword', user)
        if (code === 200) {
            formState.password = '';
            formState.newpwd = '';
            formState.pwd = '';
            localStorage.setItem('user', JSON.stringify(user));
            message.success(msg);
            router.push('/set/suser');
        }
    }


}

</script>
<style scoped lang="scss">
#mima {
    width: 100%;
    height: 93.5vh;
    background-color: #F2FBFA;
    position: relative;

    .box {
        width: 80%;
        height: 600px;
        background-color: #fff;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -60%);
        border-radius: 10px;
        box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);

        .one {
            width: 100%;
            height: 10%;
            display: flex;
            gap: 15px;
            align-items: center;
            padding: 0 20px;
            box-sizing: border-box;

            .logo {
                width: 7px;
                height: 25px;
                background-color: #3DD4A7;
                border-radius: 5px;
            }

            .title {
                font-size: 21px;
                color: #3f3f3f;
                font-weight: 500;
            }
        }

        .two {
            width: 100%;
            height: 75%;
            padding: 0 20px;
            box-sizing: border-box;
            border-top: 1px solid #EBEBEB;
            border-bottom: 1px solid #EBEBEB;
            overflow: hidden;

            .input {
                margin-top: 50px;
            }
        }

        .three {
            width: 100%;
            height: 15%;
            padding: 0 20px;
            box-sizing: border-box;
            position: relative;

            div {
                position: absolute;
                top: 5px;
                left: 0px;

                button {
                    width: 70px;
                    height: 40px;
                    border-radius: 5px;
                    background-color: #3DD4A7;
                    color: #fff;
                    font-size: 16px;
                    cursor: pointer;
                }
            }
        }
    }
}
</style>
